package com.zxp.utils.fileUtils;

import com.zxp.utils.configParseUtils.JsonConfigReader;
import me.tongfei.progressbar.ProgressBar;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class FileDownloader {


    /**
     * Download a file from the specified URL and save it to the specified destination.
     *
     * @param fileUrl     The URL of the file to be downloaded.
     * @param destination The local file path where the downloaded file will be saved.
     * @throws IOException If an I/O exception occurs during the download or file writing process.
     */
    public static void downloadFile(String fileUrl, String destination) throws IOException {
        URL url = new URL(fileUrl);
        URLConnection connection = url.openConnection();

        try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
             FileOutputStream fileOutputStream = new FileOutputStream(destination)) {

            byte[] dataBuffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                fileOutputStream.write(dataBuffer, 0, bytesRead);
            }
        }
    }

    /**
     * Downloads a file from the specified URL to the given destination path with a progress bar.
     *
     * @param fileUrl     The URL of the file to download.
     * @param destination The local path where the file will be saved.
     * @throws IOException If an I/O error occurs during the download.
     */
    public static void downloadFileWithBar(String fileUrl, String destination) throws IOException {
        URL url = new URL(fileUrl);
        URLConnection connection = url.openConnection();

        int fileSize = connection.getContentLength();  // Get the total file size
        try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
             FileOutputStream fileOutputStream = new FileOutputStream(destination)) {

            byte[] dataBuffer = new byte[1024];
            int bytesRead;
            int totalBytesRead = 0;

            try (ProgressBar progressBar = new ProgressBar("Downloading", fileSize)) {
                while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                    fileOutputStream.write(dataBuffer, 0, bytesRead);
                    totalBytesRead += bytesRead;
                    progressBar.stepBy(bytesRead);  // Update progress bar
                }
            }
        }
    }
    
    public static void main(String[] args) {
//        String fileUrl = "https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.zip";
//        String destination = "B:\\download\\downloadedFile.zip";
        String fileUrl = JsonConfigReader.getString("fileUrl");
        System.out.println(fileUrl);
        String destination = JsonConfigReader.getString("destination");
        System.out.println(destination);
        try {
            downloadFileWithBar(fileUrl, destination);
            System.out.println("File downloaded successfully.");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Error downloading file: " + e.getMessage());
        }
    }
}
